#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
const int N = 3e6 + 10;
int tre[N][62], p[N];
int idx;
int get_num(char e)
{
    if (e >= 'a' && e <= 'z')
    {
        return e - 'a';
    }
    else if (e >= 'A' && e <= 'Z')
    {
        return e - 'A' + 26;
    }
    else
    {
        return e - '0' + 52;
    }
}
void insert(string& s)
{
    int cur = 0;
    p[cur]++;
    for (auto e : s)
    {
        int path = get_num(e);
        if (tre[cur][path] == 0)
        {
            tre[cur][path] = ++idx;
        }
        cur = tre[cur][path];
        p[cur]++;
    }
}
int get(string& s)
{
    int cur = 0;
    for (auto e : s)
    {
        int path = get_num(e);
        if (tre[cur][path] == 0)
        {
            return 0;
        }
        cur = tre[cur][path];
    }

    return p[cur];
}
int main()
{
    int t = 0;
    cin >> t;
    while (t--)
    {
        for (int i = 0; i <= idx; i++)
        {
            for (int j = 0; j < 62; j++)
            {
                tre[i][j] = 0;
            }
            p[i] = 0;
        }
        idx = 0;
        int n, q;
        cin >> n >> q;
        while (n--)
        {
            string s;
            cin >> s;
            insert(s);
        }
        while (q--)
        {
            string g;
            cin >> g;
            cout << get(g) << endl;
        }
    }
    return 0;
}